package com.mine.redis.util.mapper;

import com.mine.redis.util.bean.CouponDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author weixiao
 * @date 2024/8/16 13:50
 */
@Mapper
public interface CouponMapper {

    @Select("select * from t_coupon where id = #{id}")
    CouponDO selectById(@Param("id") Integer id);

    /**
     * 优惠券库存扣减，这里where条件中count > 0采用了乐观锁的思想，防止超卖问题
     *
     * @param id 优惠券id
     * @return 更新了几张优惠券
     */
    @Update("update t_coupon set count = count - 1 where count > 0 and id = #{id}")
    Integer minusCoupon(@Param("id") Integer id);
}
